/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.util;

import java.util.Properties;

/**
 * An extension of {@link Properties} class which adds methods for accessing properties
 * of various non-{@link String} types.
 */
public class Props extends Properties {
    
    public Props() { }
    
    public Props(Properties defaults_) { super(defaults_); }

    public boolean getBoolean(String propertyName_) {
	String resultStr = null;
	try {
	    resultStr = getProperty(propertyName_);
	    Dbg.chk(resultStr, "Unknown property: " + propertyName_);
	    resultStr = resultStr.trim();
	    return resultStr.equals("1") || resultStr.equals("true");
	} catch (NumberFormatException nfe_) {
	    Dbg.fail("Non-boolean value " + resultStr + " for property " + propertyName_);
	    return false;
	}
    }

    public int getInteger(String propertyName_) {
	String resultStr = null;
	try {
	    resultStr = getProperty(propertyName_);
	    Dbg.chk(resultStr, "Unknown property: " + propertyName_);
	    return Integer.parseInt(resultStr);
	} catch (NumberFormatException nfe_) {
	    Dbg.fail("Non-integer value " + resultStr + " for property " + propertyName_);
	    return 0;
	}
    }

    public long getLong(String propertyName_) {
	String resultStr = null;
	try {
	    resultStr = getProperty(propertyName_);
	    Dbg.chk(resultStr, "Unknown property: " + propertyName_);
	    return Long.parseLong(resultStr);
	} catch (NumberFormatException nfe_) {
	    Dbg.fail("Non-long value " + resultStr + " for property " + propertyName_);
	    return 0;
	}
    }

    public float getFloat(String propertyName_) {
	String resultStr = null;
	try {
	    resultStr = getProperty(propertyName_);
	    Dbg.chk(resultStr, "Unknown property: " + propertyName_);
	    return Float.parseFloat(resultStr);
	} catch (NumberFormatException nfe_) {
	    Dbg.fail("Non-float value " + resultStr + " for property " + propertyName_);
	    return (float)0.0;
	}
    }
    
    public double getDouble(String propertyName_) {
	String resultStr = null;
	try {
	    resultStr = getProperty(propertyName_);
	    Dbg.chk(resultStr, "Unknown property: " + propertyName_);
	    return Double.parseDouble(resultStr);
	} catch (NumberFormatException nfe_) {
	    Dbg.fail("Non-double value " + resultStr + " for property " + propertyName_);
	    return 0.0;
	}
    }

    public boolean defined(String propertyName_) { return getProperty(propertyName_)!=null; }

    public boolean getBoolean(String propertyName_, boolean default_) {
	return defined(propertyName_) ? getBoolean(propertyName_) : default_;
    }

    public int getInteger(String propertyName_, int default_) {
	return defined(propertyName_) ? getInteger(propertyName_) : default_;
    }
    
    public long getLong(String propertyName_, long default_) {
	return defined(propertyName_) ? getLong(propertyName_) : default_;
    }
    
    public float getFloat(String propertyName_, float default_) {
	return defined(propertyName_) ? getFloat(propertyName_) : default_;
    }
    
    public double getDouble(String propertyName_, double default_) {
	return defined(propertyName_) ? getDouble(propertyName_) : default_;
    }

    public void setProperty(String propertyName_, boolean value_) {
	setProperty(propertyName_, String.valueOf(value_));
    }

    public void setProperty(String propertyName_, int value_) {
	setProperty(propertyName_, String.valueOf(value_));
    }
    
    public void setProperty(String propertyName_, long value_) {
	setProperty(propertyName_, String.valueOf(value_));
    }
    
    public void setProperty(String propertyName_, float value_) {
	setProperty(propertyName_, String.valueOf(value_));
    }
    
    public void setProperty(String propertyName_, double value_) {
	setProperty(propertyName_, String.valueOf(value_));
    }
 }

    
